package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Menu;

import com.itheima.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
@Slf4j
public class MenuController {
    @Reference
    private MenuService menuService;

    @RequestMapping("/add")
//    @PreAuthorize("hasAuthority('MENU_ADD')")
    public Result add(@RequestBody Menu menu){
        log.debug("controller:{"+menu+"}");
        menuService.add(menu);
        log.debug("检查项添加成功！！");
        return new Result(true, MessageConst.ADD_MENU_SUCCESS);
    }

    @RequestMapping("/findByPage")
//    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        log.debug("controller{"+queryPageBean+"}");
        PageResult pageResult = menuService.findByPage(queryPageBean);
        log.debug("检查项分页查询成功!!");
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, pageResult);
    }


    @RequestMapping("/findById")
//    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public Result findById(Integer id){
        log.debug("CheckItemController：findById:" + id);
        Menu menu = menuService.findById(id);
        log.debug("根据id查询检查项成功!!");
        return new Result(true, MessageConst.QUERY_MENU_SUCCESS, menu);
    }

    @RequestMapping("/edit")
//    @PreAuthorize("hasAuthority('MENU_EDIT')")
    public Result edit(@RequestBody Menu menu){
        log.debug("CheckItemController:edit: " + menu);
        menuService.edit(menu);
        log.debug("检查项修改成功!!");
        return new Result(true, MessageConst.EDIT_SUCCESS);
    }

    @RequestMapping("/delById")
//    @PreAuthorize("hasAuthority('MENU_DELETE')")
    public Result delById(Integer id){
        log.debug("CheckItemController:delete: " + id);
        menuService.delById(id);
        log.debug("检查项删除成功!!");
        return new Result(true, MessageConst.DELETE_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findAll")
//    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public Result findAll(){
        log.debug("CheckItemController:findAll");
        List<Menu> menusList = menuService.findAll();
        log.debug("检查项查询所有成功!!");
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, menusList);
    }
}
